--[[
  浙商银行核心交易Trade协议解析
]]
require("apm_core")
require("ApmXml")

local G_DEBUG = false

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local t_xml_tree
local s_no
local s_serviceId
local s_serviceId_name
local s_rspcode
local s_rspmsg

local t_serviceId = {
    atmaQK = "atm取款（发卡）",
    atmaRZR = "atm转入确认（发卡）",
    atmaZC = "atm转出（发卡）",
    atmaZR = "atm转入（发卡）",
    atmCK = "atm存款",
    atmCKJC = "atm存款检查",
    atmCKQR = "atm转入确认",
    atmCZ = "atm冲正",
    atmiCKCX = "存款撤销",
    atmiCXCZ = "存款撤销冲正",
    atmiQK = "atm取款（受理，他行卡在本行ATM）",
    atmiZC = "atm发卡转出",
    atmiZR = "atm发卡转入",
    atmiZRQR = "atm发卡转入确认",
    atmJC = "atm加钞",
    atmKH = "atm开户",
    atmXGMM = "atm修改密码",
    atmYECX = "atm余额查询",
    atmZRQR = "atm转入确认",
    atmZZ = "atm转账（本转本）",
    atmZZJK = "atm总账",
    cl23CDSC = "23磁道生成",
    clAJFK = "按揭放款",
    clAJHK = "按揭还款",
    clATMFX = "atm转入确认",
    clATMSX = "atm修改密码",
    clBDHZH = "不动户转回",
    clBDHZR = "不动户转入",
    clBEPSLZ = "二代支付小额来账",
    clBEPSWZ = "二代支付小额往账",
    clBHHD = "行内直接汇款",
    clBHZZ_ST = "本行转账受托支付",
    clBHZZYW = "处理本行转帐帐务处理",
    clBNWLX = "表内外利息冲减",
    clBWJFYW = "报文解付",
    clBWLRJF = "报文解付",
    clBWLRYW = "报文录入",
    clBWZ = "表外账务处理",
    clBZJDJ = "保证金冻结",
    clBZJJD = "处理保证金解冻",
    clBZJJWH = "处理保证金借据维护",
    clBZJXJ = "处理保证金销记",
    clCBSIDL = "未说明",
    clCBSMOZ = "未说明",
    clCDDPQF = "电子承兑汇票签发保证金及存单处理",
    clCDDPTH = "电子承兑汇票退回保证金及存单处理",
    clCDHPHX = "未说明",
    clCDHPQF = "承兑汇票签发",
    clCDHPSK = "处理承兑汇票收款",
    clCDHPTP = "承兑汇票退回",
    clCDKDJ = "处理长短款登记",
    clCDKH = "处理存单开户",
    clCDKXJ = "处理长短款销记",
    clCDXH = "存单销户",
    clCDXPQF = "批量签发银票单笔服务",
    clCKHYKH = "存款合约（账号）开户",
    clCKHYXH = "存款合约（账号）销户",
    clCKYHFK = "出口押汇类放款",
    clCKYHHK = "出口还款",
    clCKYW = "处理存款账务",
    clCNAPLZ = "二代支付大额来帐记账服务",
    clCNAPMZ = "二代支付抹帐记账服务",
    clCNAPQS = "处理二代支付清算",
    clCNAPWZ = "二代支付大额往帐记账服务",
    clCRDZCZ = "电子存在柜面存入",
    clCUPSTT = "处理银联清算",
    clCWXTJZ = "新财务系统记账接口",
    clCXCFFS = "未说明",
    clCZCDKL = "处理存折存单批量开立",
    clCZKH = "存折开户",
    clDBDKFK = "进口打包类放款",
    clDBDKHK = "打包贷款还款",
    clDBG = "抵质押代保管",
    clDBKZ = "单边扣账处理+销记凭证",
    clDBPKZ = "处理担保品控制",
    clDEQS = "处理大额清算",
    clDETCWH = "处理大额头寸维护",
    clDEWZJZ = "大额往帐记账服务",
    clDEZF_ST = "大额支付受托支付",
    clDGCDBT = "存单部提",
    clDGCDBT_TY = "对公存单部提",
    clDGCDXH = "对公存单销户",
    clDGCDXH_LC = "理财申购撤销存单销户",
    clDGCDXH_TY = "对公存单销户",
    clDJGSYW = "账户凭证挂失",
    clDJJGYW = "处理冻结解挂业务",
    clDKHXYW = "贷款核销",
    clDKHYKH = "贷款合约（账号）开户",
    clDKHYXH = "贷款合约（账号）销户",
    clDKLLTZ = "贷款利率调整（授信）",
    clDPLXJS = "电子银行汇票利息计算",
    clDQCDXH = "定期存单销户处理",
    clDQZYCZ = "定期质押处置",
    clDQZYDJ = "定期质押登记",
    clDSCDBT = "对私存单部提",
    clDSCDXH_LC = "理财申购撤销存单销户",
    clDSCDZC = "个人定期柜面转存",
    clDSHYKH = "个人合约（账号）开户",
    clDSHYXH = "个人合约（账号）销户",
    clDWZHKL = "处理单位帐户开立",
    clDWZHXH = "单位账户销户",
    clDZCZLC = "电子存折理财",
    clDZHP_ST = "电子汇票签发受托支付",
    clDZHPCZ = "电子银行汇票处置",
    clDZHPDF = "电子银行汇票兑付账务处理",
    clDZHPJZ = "电子银行汇票结转账务处理",
    clDZHPQF = "电子银行汇票签发账务处理",
    clDZHPTH = "电子银行汇票未用退回账务处理",
    clDZJFZZ = "电子缴费转账",
    clDZPJSK = "处理电子票据备款",
    clDZZZ = "处理定制转帐",
    clEZHKL = "网银账户开立",
    clEZHXH = "网银账户销户",
    clFBZCZW = "非标资产账务处理",
    clFKSTZF = "受托支付协议",
    clFZDSXF = "非自动收费",
    clGGXYCX = "未说明",
    clGGXYQY = "未说明",
    clGJJFF = "公积金放款",
    clGJJHK = "公积金还款",
    clGMTQS = "未说明",
    clGRCZYW = "隔日冲补账业务处理",
    clGYJL = "处理柜员建立",
    clGYQXLJ = "未说明",
    clGYSXYW = "处理柜员收现业务",
    clHB = "换折换存单换本换卡换证实书",
    clHBPQF = "本票汇票签发账务处理",
    clHBPQF_1 = "本票汇票签发账务处理(已预约)",
    clHBPTH = "汇本票未用退回",
    clHPDYK = "处理汇票多余款",
    clHPJFYW = "汇票解付业务",
    clHPZJYC = "汇票资金移存",
    clIBPSCZ = "IBPS 系统冲正服务",
    clIBPSQS = "IBPS系统清算",
    clJBZJXJ = "处理旧保证金销记",
    clJGJL = "处理机构建立",
    clJGQD = "机构签到",
    clJGQT = "机构签退",
    clJGZHXY = "监管账户协议签订",
    clJGZHZW = "监管账户账务处理",
    clJHHWH = "交换号维护",
    clJSFYW = "处理缴税费业务",
    clJSH = "处理结售汇(结汇、售汇、套汇)",
    clJSTDF = "处理结算通兑付",
    clJSTTG = "处理结算通托管",
    clJSTZF = "处理结算通支付",
    clJSTZYW = "积数调整",
    clJSZZBZ = "处理即时转账补账",
    clJWHC = "境外汇出",
    clJWHRRZ = "境外汇入入账",
    clJWTH = "境外汇入汇款入账处理",
    clJWXG = "外汇汇出汇款修改",
    clJYYW = "通用校验",
    clKHPZQY = "客户凭证启用",
    clKHZDSC = "卡号自动生成",
    clKKH = "处理开卡",
    clKKSQ = "单笔开卡申请",
    clKZXH = "卡折销户处理",
    clLXJSBP = "计算本票利息处理",
    clLXJSBZ = "保证金利息计算",
    clLXJSDG = "对公利息试算",
    clLXJSDK = "clLXJSDK",
    clLXJSDS = "对私利息试算",
    clLXJSFQ = "clLXJSFQ",
    clLXJSZH = "不动户利息",
    clLZGZRZ = "来帐挂帐入帐记账服务",
    clMMCZ = "密码重置",
    clMMJS = "密码解锁",
    clMYSC = "未说明",
    clNBHXG = "内部户修改",
    clNBKLPL = "处理内部开立批量",
    clNBZHKH = "内部账户开立（非录入审批模式）",
    clNBZHXH = "处理内部户销户",
    clPJBD = "票据补打",
    clPJGSCX = nil,
    clPJGSTC = "票据挂失提存",
    clPJGSYW = "票据挂失",
    clPJXTJZ = "票据系统账务接口",
    clPJYWTJ = "处理票据业务统计",
    clPLZZYW = "批量转账业务",
    clPTDKBK = "贷款备款",
    clPTFK = "普通贷款放款",
    clPTHK = "普通贷款还款",
    clPZDSXF = "未说明",
    clQKYW = "处理取款业务",
    clQKYW_YY = "预约取款",
    clQXJL = "未说明",
    clQXXJ = "未说明",
    clRZDKYE = "融资贷款余额处理",
    clSFCXZW = "司法查询账务处理",
    clSFDJ = "司法冻结处理",
    clSFKHYW = "司法扣划处理",
    clSHGCQS = "上海综合业务系统轧差清算服务",
    clSHTCJZ = "上海综合业务系统记账服务",
    clSHTZCZ = "上海综合业务系统调账冲正服务",
    clSHZHRZ = "上海综合业务系统日终清算服务",
    clSKLCCD = "商卡存单理财",
    clSKSXF = "商卡手续费(年费)",
    clSKYC = "商卡移出",
    clSSQSNB = "宁波实时清算",
    clSTDY = "收妥抵用业务处理",
    clSXFJS = "自动手续费计算",
    clSXZDZ = "其他授信",
    clSZTCJZ = "深圳同城记账程序",
    clSZTZCZ = "深圳同城调账记账服务",
    clTCJHJS = "同城交换结束处理",
    clTCJZ = "cpsp同城系统记账服务",
    clTCNBJZ = "宁波通存记账",
    clTCQSYW = "同城清算业务(人行)",
    clTCTCDF = "同城提出贷方",
    clTCTCJF = "同城提出借方",
    clTCTCTP = "同城提出退票业务处理",
    clTCTRDF = "同城提入贷方业务处理",
    clTCTRJF = "同城提入借方业务处理",
    clTCTRTP = "同城提入退票业务处理",
    clTCXTQS = "同城提出系统清算",
    clTIPSJK = "财税库银柜面收款",
    clTRXTQS = "同城提入系统清算",
    clTSYWDJ = "托收业务登记处理",
    clTSYWXJ = "托收业务销记处理",
    clTYLXJS = "同业利息计算",
    clTYZJRZ = "授信同业资金入账",
    clTYZPBD = "clTYZPBD",
    clTYZPTP = "同业支票退票",
    clWDGRYC = "处理网点隔日异常",
    clWHPLZZ = "外汇批量转帐",
    clWLTYTZ = "往来通用调账记账服务",
    clWTCRDZ = "委托存入电子存折",
    clWZTCQS = "温州同城清算业务",
    clXCKYW = "新存款业务处理",
    clXDCCXK = "未说明",
    clXDCKCX = "未说明",
    clXDCKXY = "未说明",
    clXDKFK = "新贷款放款",
    clXDZCRC = "信贷资产入池",
    clXEDSXF = "未说明",
    clXEGZRZ = "小额挂账入账",
    clXERZQS = "小额日终清算服务",
    clXESSTZ = "小额实时调账服务",
    clXETCJZ = "小额通存往帐记账服务",
    clXEWZJZ = "小额往帐记账服务",
    clXEZBJZ = "小额组包记账服务",
    clXJDBKZ = "未说明",
    clXKZXH = "新卡折销户处理",
    clXQKYW = "新取款业务处理",
    clXSKLC = "新商卡理财",
    clXYKZW = "处理信用业务记账",
    clYBDD = "未说明",
    clYBTCK = "未说明",
    clYBTDY = "一本通还本打印",
    clYBTYC = "一本通移出处理",
    clYBTYR = "一本通移入处理",
    clYCDQSK = "处理单笔承兑汇票备款",
    clYJHK = "应解汇款转入",
    clYJHKBB = "未说明",
    clYJHKFK = "应解汇款付款",
    clYJHKFX = "应解汇款付现",
    clYJJKCK = "处理夜间金库",
    clYJSHCJ = "远期结售汇成交",
    clYJSHJG = "远期结售汇交割",
    clYJSHZQ = "远期结售汇展期",
    clYJZJFK = "授信应急放款",
    clYLGCX = "处理银联柜面通",
    clYPAYSC = "生成承兑汇票暗印",
    clZDSFZW = "自动收费类账务处理",
    clZDSXF = "自动类手续费收取",
    clZDZFF = "准贷证发放",
    clZFTCWH = "二代支付特色头寸维护服务",
    clZFZPQC = "二代支票圈存服务",
    clZHDJJD = "账户冻结解冻",
    clZHFX = "账户付息",
    clZHJKGL = "账户监控管理",
    clZHKMJZ = "账户科目结转",
    clZHPLZF = "处理账户批量止付",
    clZHQYTY = "账户启用停用",
    clZHSD = "账户锁定",
    clZHSJJ = "单位账户升降级",
    clZHTBK = nil,
    clZHZF = "帐户止付解止付",
    clZJCJFK = "资金上存下借处理",
    clZJCJHK = "资金存借还款处理",
    clZJCJLR = "系统内资金调拨录入",
    clZJYWZW = "中间业务平台账务接口",
    clZKBWZ = "重空表外账务处理",
    clZKLJCS = "未说明",
    clZNFQFK = "智能分期放款",
    clZNFQHK = "智能分期还款",
    clZPPJSY = "支票使用",
    clZPPZCS = "支票凭证出售",
    clZPPZZX = "支票凭证注销",
    clZPQCQX = "处理支票圈存取消",
    clZPYXCX = "支票影像提出提入冲正销记服务",
    clZPYXDJ = "支票影像登记",
    clZPYXXJ = "支票影像提入销记服务",
    clZWZHKL = "指纹账户开立",
    clZWZHXH = "指纹账户销户",
    clZXGRYC = "小额隔日异常处理（中心）",
    clZXXEYC = "小额异常处理（中心）",
    clZYFQHK = "自由分期还款",
    clZZLSTD = "处理本行转帐帐务处理-绿色通道",
    cxJYLS = "查询交易流水信息",
    cxKXHXX = "查询开销户信息",
    cxNBZH = "查询内部帐户",
    cxSKJJGN = "查询商卡功能",
    cxTCJHXX = "查询同城交换信息",
    cxTZYYXX = "查询通知预约信息",
    cxYBTXX = "查询一本通信息",
    cxZJTCXX = "查询资金头寸信息",
    cxZPQCXX = "查询支票圈存信息",
    hqBDHZT = "获取不动户状态",
    hqBZJMX = nil,
    hqCDHPQD = "获取银行承兑汇票清单信息",
    hqCDKXX = "获取长短款信息",
    hqCSKYMX = "获取财税库银明细",
    hqCZWDX = "存折补打",
    hqDGZHTZ = "未说明",
    hqDGZSSH = "获取对公证实书号",
    hqDSMXZ = nil,
    hqDZJFXX = "获取电子缴费协议",
    hqDZZZXX = "获取定制转账信息",
    hqFZBDXX = "获取电子存折绑定信息",
    hqFZBDXX_E = "获取电子存折绑定信息(网银专用)",
    hqFZMXLX = "电子存折利息清单补打",
    hqGDXX = "获取挂失冻结信息",
    hqGGJBXX = "hqGGJBXX",
    hqGGXTCS = "获取公共系统参数",
    hqGGXYXX = "未说明",
    hqHDLSXZ = "未说明",
    hqHDQD = "行内客户汇兑信息",
    hqHDXX = "获取汇兑信息",
    hqHL = "获取汇率",
    hqJJJYMX = "基金明细查询",
    hqJJQYXX = "基金签约查询",
    hqJSTXX = "获取结算通信息",
    hqJWHDQD = "境外客户汇兑信息",
    hqJWHDXX = "获取境外汇兑信息",
    hqKHPJXX = "获取客户票据信息",
    hqKHXXXX = "获取客户详细信息",
    hqKHXXZH = "获取客户及账户信息",
    hqKJKMXX = "获取会计科目信息",
    hqLLXX = "获取利率信息",
    hqLSFLLS = "获取分录流水信息",
    hqLSLFXX = "累收累付信息获取",
    hqNBSSQS = "获取宁波实时清算信息",
    hqNBZHH = "返回内部账户号",
    hqPJGSXX = nil,
    hqQSZHXX = "获取清算信息",
    hqSFCS = "heping",
    hqSFCXDJ = "未说明",
    hqSKZZH = "获取商卡子账户",
    hqSMKXX = "获取睡眠卡信息",
    hqSXFDJ = "获取手续费登记信息",
    hqSYHKXX = "获取上月还款信息",
    hqTCCCXX = "获取同城交换场次信息",
    hqTCJYKZ = "获取同城交易控制信息",
    hqTCJYXX = "获取提出交易信息（根据受理编号）",
    hqTCJZJG = nil,
    hqTCQSSJ = "获取同城清算数据",
    hqTCTRTC = "获取同城提入提出数据(统计)",
    hqTCWDQS = "获取同城网点清算信息",
    hqTCXX = "获取头寸信息",
    hqTCZXQS = "获取同城中心清算信息",
    hqTSYWXX = "获取托收业务信息",
    hqTYZPXX = "获取同业支票信息",
    hqWZTCQS = "获取温州同城清算信息",
    hqXEQSYE = "获取小额清算余额",
    hqYHCDHP = "获取银行承兑汇票信息",
    hqYJHKXX = "获取应解汇款信息",
    hqYJSHDJ = "获取远期结售汇登记信息",
    hqYXYWXX = "获取核心影像业务信息",
    hqZDZXX = "获取准贷证信息",
    hqZFHXXX = "二代支付",
    hqZHGSXX = nil,
    hqZHQYZT = "未说明",
    hqZHXX = "获取账户信息",
    hqZHYW = "获取账号业务列表",
    hqZJCJXX = "获取系统内资金调拨信息",
    hqZJTCXX = "获取资金头寸信息",
    hqZPPJXX = "获取支票票据信息",
    hqZPQCXX = "获取支票圈存信息",
    hqZYDJXX = "获取质押登记信息",
    jlDZCXCF = "登记支付系统查询查复",
    jlHKXX = "记录换卡信息",
    jlKHXXXX = "记录开户信息",
    jlSFCZXX = "司法操作详情录入",
    jlTZYYXX = "记录通知预约信息",
    jlZHYCXX = "记录账户异常信息",
    jy23CD = "校验23磁道",
    jyBZJDJB = "校验保证金登记簿",
    jyCDMM = "磁道密码校验(cpsp同城使用)",
    jyCZYXX = "校验柜员信息",
    jyDGZHXG = "未说明",
    jyFZZHJY = "未说明",
    jyJGQT = "校验机构签退",
    jyJGZW = "校验机构账务",
    jyKHSCYW = "客户删除校验",
    jyMM = "校验密押",
    jyMMJY = "校验密码",
    jyMY = "校验密押",
    jyTRTCSJ = "可对提入提出汇总数据",
    jyYCHPZJ = "校验移存汇票资金移存数据",
    jyZFMM = "校验支付密码",
    jyZHRZZT = "未说明",
    jyZHXHYW = "账户销户校验",
    posaJSTZ = "pos预授权完成(离线)",
    posaSK = "电话pos收款",
    posaSQWC = "pos预授权完成(联机)",
    posaWCCX = "pos预授权完成撤销(受理)",
    posaWCCZ = "pos预授权完成冲正(受理)",
    posaWXCZ = "pos预授权完成撤销冲正(受理)",
    posaXF = "pos受理消费",
    posaXFCC = "pos受理消费撤销冲正",
    posaXFCX = "pos受理消费撤销",
    posaXFCZ = "pos受理消费冲正",
    posaXFTH = "pos受理消费退货",
    posHYCZ = "会员卡充值",
    posHYJF = "会员卡缴年费",
    posHYKK = "会员卡开卡",
    posHYXF = "会员卡消费",
    posiCKZH = "商户账户验证",
    posiCXCZ = "pos预授权撤销冲正(发卡)",
    posiJSTZ = "pos结算通知",
    posiSQCX = "pos预授权撤销(发卡)",
    posiSQCZ = "pos预授权冲正(发卡)",
    posiSQWC = "pos预授权完成(联机)",
    posiWCCX = "pos预授权完成撤销(发卡)",
    posiWCCZ = "pos预授权完成冲正(发卡)",
    posiWTGX = "建立委托关系",
    posiWXCZ = "pos预授权完成撤销冲正(发卡)",
    posiXF = "pos发卡消费",
    posiXFCC = "pos发卡消费撤销冲正",
    posiXFCX = "pos发卡消费撤销",
    posiXFCZ = "pos发卡消费冲正",
    posiXFTH = "pos发卡消费th",
    posiYSQ = "pos预授权(发卡)",
    posiZJCZ = "pos追加预授权冲正(发卡)",
    posiZJSQ = "pos追加预授权(发卡)",
    posYECX = "pos余额查询",
    posZZ = "pos行内转账",
    posZZCZ = "本行转账冲正(受理)",
    scKh = "删除客户",
    scKH = "删除客户",
    xgCKZHXX = "修改存款账户信息",
    xgDGZHXX = "修改对公帐户信息",
    xgDJGYFK = "修改柜员放款参数",
    xgDJHKZH = "修改还款组信息",
    xgDKZHXX = "修改贷款帐户信息",
    xgDSZHXX = "修改对私帐户信息",
    xgGGCSB = "公共参数表维护",
    xgJGXX = "机构维护",
    xgJHCCXX = "更新交换场次信息",
    xgJSTZH = "修改结算通帐户",
    xgJTSJCS = "计提税金参数修改",
    xgKHXXXX = "修改客户详细信息",
    xgKMZDXX = "修改科目信息",
    xgNBZHLL = "批量修改内部账户利率",
    xgNBZHXX = "修改内部帐户信息",
    xgSFCXXX = "司法查询撤销",
    xgSFDJXX = "司法冻结撤销",
    xgSKDKGN = "商卡贷款功能修改",
    xgSKJJGN = "修改商卡理财功能",
    xgSKXE = "xuning",
    xgSMKXX = "睡眠卡恢复",
    xgTCJYKZ = "修改同城交易控制信息",
    xgTCJYXX = "更新提出交易信息(根据业务编号)",
    xgTZYYXX = "修改通知预约信息",
    xgYJJKKT = "夜间金库开通修改",
    xgZHKHXX = "修改账号客户信息",
    xgZHYCXX = "修改账户异常信息",
}

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

--- 检测是否属于Trade协议
-- return
-- true; false
local function protocol_detect(s_payload)
    if s_payload == nil then return false end
    t_xml_tree = ApmXml.load(s_payload)
    return ApmXml.getValue(t_xml_tree, "/Trade") ~= nil
end

--- 解析请求
-- return
-- true: 解析成功; false: 解析失败
local function parse_request(ApmData)
    -- 流水号
    s_no = ApmXml.getValue(t_xml_tree, "/Trade/RequestHeader/businessAcceptId/text()")
    if s_no ~= nil then
		s_no = string.trim(s_no)
		ApmData.replaceField("transaction.code", s_no)
	end
	
    -- 交易类型
    s_serviceId = ApmXml.getValue(t_xml_tree, "/Trade/RequestHeader/serviceId/text()")
    if s_serviceId ~= nil then
		s_serviceId = string.trim(s_serviceId)
        s_serviceId_name = t_serviceId[s_serviceId]
        if s_serviceId_name == nil then s_serviceId_name = s_serviceId end
        ApmData.replaceField("transaction.type_code", s_serviceId)
        ApmData.replaceField("transaction.type_name", s_serviceId_name)
        ApmData.replaceField("transaction.title", s_serviceId_name)
    end

    return true
end

--- 解析响应
-- return
-- true: 解析成功; false: 解析失败
local function parse_response(ApmData)
    s_rspcode = ApmXml.getValue(t_xml_tree, "/Trade/ResponseHeader/errorNo/text()")
    if s_rspcode ~= nil then
        s_rspcode = string.trim(s_rspcode)
        ApmData.replaceField("response.code_string", s_rspcode)
        ApmData.delFields("response.code")
    end

    s_rspmsg = ApmXml.getValue(t_xml_tree, "/Trade/ResponseHeader/errorInfo/text()")
    if s_rspmsg ~= nil then
        s_rspmsg = string.trim(s_rspmsg)
        ApmData.replaceField("response.error_message", s_rspmsg)
    end

    return true
end

-- return
function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 1435 then return end
    if ApmData.getMsgType() ~= 1 then return end

    local s_commarea_req
    local s_commarea_resp

    s_commarea_req = ApmData.getField("request.dts.service.arg.1", 1)
    if protocol_detect(s_commarea_req) then
        parse_request(ApmData)
    else
--        logInfo("REQ:[%s]", s_commarea_req)
        return
    end

    s_commarea_resp = ApmData.getField("response.dts.service.return.1", 1)
    if protocol_detect(s_commarea_resp) then
        --parse_request(ApmData)
        parse_response(ApmData)
    else
--        logInfo("RESP:[%s]", s_commarea_resp)
        return
    end

    ApmData.replaceField("request.dts.service.arg.1", s_commarea_req)
    ApmData.replaceField("response.dts.service.return.1", s_commarea_resp)

    if s_serviceId == nil then
        logInfo("s_serviceId is nil REQ:[%s]", s_commarea_req)
    elseif s_rspcode == nil then
        logInfo("s_rspcode is nil RESP:[%s]", s_commarea_resp)
    else
	--[[
		logInfo("CICS(%s:%d->%s:%d) serviceId:%s|name:%s|seq:%s|ret:%s|%s|Request:["..KBLUE.."%s"..RESET.."]\nResponse:["..KBLUE.."%s"..RESET.."]\n",
            parseIp(ApmData.getSrcIp()),
            ApmData.getSrcPort(),
            parseIp(ApmData.getDstIp()),
            ApmData.getDstPort(),
            s_serviceId,
            s_serviceId_name,
            s_no,
            s_rspcode,
            s_rspmsg,
			s_commarea_req,
			s_commarea_resp
        )
	]]
        logInfo("CICS(%s:%d->%s:%d) serviceId:%s|name:%s|seq:%s|ret:%s|%s\n",
            parseIp(ApmData.getSrcIp()),
            ApmData.getSrcPort(),
            parseIp(ApmData.getDstIp()),
            ApmData.getDstPort(),
            s_serviceId,
            s_serviceId_name,
            s_no,
            s_rspcode,
            s_rspmsg
        )

    end

    return
end
--v1.0.5
